package com.ipay.service.impl;

import com.alibaba.fastjson.JSON;
import com.ipay.core.HttpGateway;
import com.ipay.core.HttpRequest;
import com.ipay.core.Param;
import com.ipay.pabcsEntity.PaylistEntity;
import com.ipay.service.ApiPabcsService;
import com.ipay.utils.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service("apiPabcsService")
public class ApiPabcsServiceImpl implements ApiPabcsService {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private HttpGateway gateway;

    private static String errcode_success="0";

    /**http://localhost:8081/payorder/infoPabcsPay
     * open_id,timestamp,sign,data
     *
     * timestamp (int) (System.currentTimeMillis() / 1000)
     * open_id 商户门店 open_id
     * data pmt_type=2,3,4
     * @param mchPayKey //秘钥  open_key
     * @param payChannelNo //商户号  open_id
     * @param data //数据  data 字段 json 数据
     * @param postUrl  请求地址
     * @return
     */
    @Override
    public Map<String, Object> pabcsPayPost(String mchPayKey, String payChannelNo, String data, String postUrl) {
        Map<String, Object> wxPayMap=new HashMap<String,Object>();
        wxPayMap.put("code", 500);
        //open_id=87f46f5db1b65f0ac195285f49ee6958&timestamp=1497957842&data=3304A5EC2CDEB4D12ED3338B5C01BF6B
        Map<String, String> map=new HashMap<String, String>();
        map.put("open_id",payChannelNo);
        map.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
        String encryptStr = AESUtils.encryptParseByte2HexStr(data, mchPayKey);
        map.put("data", encryptStr);
        map.put("open_key",mchPayKey);
        //按 ascii 码 A-z 排序
        String strtosign = MapUtils.formatUrlMap(map, false, false);
        gateway.url = postUrl;
        //TODO 查询商户相关数据
        //sha1
        String strSHA1 = SHA1Utils.getSHA1(strtosign);
        //MD5
        String signstr = MD5.sign(strSHA1, "", "utf-8");
        map.put("sign", signstr);
        map.remove("open_key");
        Param param = Param.create();
        param.putAll(map);
        logger.info("============:"+JSON.toJSONString(param));
        // 银行预支付接口
        ////{"data":"4C53A9CD332858B3E9DBA225B75325926EBBC0790486E9F6E94169847CA1BA48",
        // "open_id":"7a217a35254469ecb69f8d7161b0f611","sign":"cf27f3ffac36f8eb2fd5257ef40e8ab7",
        // "open_key":"2b125c12e7f2de366dc376b4d330bd01","timestamp":"1514882564"}
        //Param resultparam = gateway.post(param);
        String postParam = MapUtils.formatUrlMap(map, false, false);
        String resultparam = HttpRequest.sendPost(postUrl, postParam);
        if(resultparam != null){
            JSONObject jsStr = JSONObject.fromObject(resultparam); //将字符串{“id”：1}
            PaylistEntity paylistEntity= (PaylistEntity)JSONObject.toBean(jsStr,PaylistEntity.class);
            System.out.println(paylistEntity.getErrcode());
            System.out.println(paylistEntity.getData());
            if(paylistEntity!=null&&StringUtil.isNotEmpty(paylistEntity.getErrcode())&&errcode_success.equals(paylistEntity.getErrcode())){
                String parseHexStr = AESUtils.parseHexStr2Byte(paylistEntity.getData(), mchPayKey);
                JSONObject jsDataStr = JSONObject.fromObject(parseHexStr); //将字符串{“id”：1}
                PaylistEntity paylistEntityData= (PaylistEntity)JSONObject.toBean(jsDataStr,PaylistEntity.class);
                System.out.println(paylistEntityData.getPmt_name());
            }
        }
        logger.info("============:resultparam----->" + resultparam);
        /*{"data":"C720595A016037DB8D5095D48C8969923622E0C78A034818046BBC657C2FC97D1212E2C4DBD0E4FD8F0B99B668C6DEEE2D3D30EC676CC60AD642ECAA535533B78CC77CF2256362EF6BB4BA247E644CEAC6FEBE77C55B3EABAE920F95D3AA48DA1B7E581575E9C799173C4775052B348C982E8E13B5E0D613A556A1810DBF8BD9AE8138E61FA642FCB30007B6CE54B4A387994F8EB7CF596255F12D74E88F3BBA4E8FEB03B84B388165C139306B3E4C4CD26E90BFAC1B2341AF94455AC828EEABE3BCFE14AB84BEA2B952A5B103407C7029AC4BCA0F50B99C0C20A1D1C663EF5BDDFAEA39F5762CA7D2E433C5B3DBAC907373B3A4BC7E8E66C2C70E635F9ADAA3B3AAD601A8F54B6177502E7B1669E36FAEE1EE21FAA4D863B5BB84A22FECA4F85D668C3D62977DA6F405C0041A8AAE386EA86231E587C4053A95D2D3D7C47D921890AF6ECBDCBC57614B16CC70352A34E5BD9D637FA4BB67141EABB28B7806CBAC0664D8A9017A688B8041C6C967A2B86EB6FA1358A282CB7B7884D1ED5149E9BF08681BFA16454B5B85C4F674396D0F8EF0729A4EA123B789CC4E1A5C6AA62AABB2CBAAA3A7B48C83985CC5A5B2FDD224D2566E98E4C152946039890AE5451A801C2536F1ACCAD0C9DEDCEBD5D1549FED1BE5C6E205C32ADA68BC75727ED565202C583E27B095AB87EE9B40380D35A71D0DB3874103C7C76E00CA5A282F1F6074DAEF8FD0A2C8D627646B12A48F05072211D4807E6CD68317B2E238450F91A5534C275210F455ADC4663656A6DF45CA695DB3E7C98434E60B98D57C1543D257E62529FD577D4B4B39020E29786317C43B6FA9900AFCD425A75F3DD6AA06784FEBFD7F35DA8782CD4234EA1B875912C5683BAD37ED4EC01A40710B726121711E84FD9BA058B3165B9FAB99DD175A0DE3703715CCC39B3A2ACDA7A82DA3845FA246DF965D15A318AEA7A41B749D0F9ED1",
                "errcode":0,"msg":"ok","timestamp":1514883783,"sign":"3a1e93f08f59a3fbb78683192ea3dfb5"}*/
        return wxPayMap;
    }
}
